package com.zzk.lanouhn.myapplication1.adapters;

import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.zzk.lanouhn.myapplication1.R;
import com.zzk.lanouhn.myapplication1.intefaces.NewsItemCliclListener;
import com.zzk.lanouhn.myapplication1.modles.AllNews;

import java.text.SimpleDateFormat;
import java.util.List;

/**
 * Created by lanouhn on 16/8/24.
 */
public class ScrollRecyclerViewAdapter extends RecyclerView.Adapter<ScrollRecyclerViewAdapter.ViewHolder>{

    private Context context;
    private List<AllNews.Datas.DataBean> allNews;
    private NewsItemCliclListener newsItemCliclListener;


    public void setScrollRecyclerViewAdapter(NewsItemCliclListener newsItemCliclListener) {
        this.newsItemCliclListener = newsItemCliclListener;
    }

    public ScrollRecyclerViewAdapter(Context context, List<AllNews.Datas.DataBean> allNews) {
        this.context = context;
        this.allNews = allNews;
    }

    /**
     * 绑定item布局
     * @param parent
     * @param viewType
     * @return
     */
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View view= LayoutInflater.from(context).inflate(R.layout.item_scroll_news,null);
        ViewHolder holder=new ViewHolder(view);
        return holder;
    }

    /**
     * 绑定数据
     * @param holder
     * @param position
     */
    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        AllNews.Datas.DataBean bean=allNews.get(position);
        holder.tv_content_item.setText(bean.getTitle());
        holder.tv_author_item.setText(bean.getUser().getName());
        Picasso.with(context).load(bean.getFeatureImg()).into(holder.iv_imgage_item);

        /**
         * 解析时间
         */
        long date=bean.getPublishTime();
        SimpleDateFormat dateFormat=new SimpleDateFormat("MM-dd HH:mm");
        String str = dateFormat.format(date);
        holder.tv_time_item.setText(str);
        if (bean.getColumnId().equals("23")) {
            holder.tv_style_item.setText(bean.getColumnName());
            holder.tv_style_item.setTextColor(Color.CYAN);
        }
        if (bean.getColumnId().equals("67")) {
            holder.tv_style_item.setText(bean.getColumnName());
            holder.tv_style_item.setTextColor(Color.GREEN);
        }
        if (bean.getColumnId().equals("68")) {
            holder.tv_style_item.setText(bean.getColumnName());
            holder.tv_style_item.setTextColor(Color.CYAN);
        }
        if (bean.getColumnId().equals("69")){
            holder.tv_style_item.setText(bean.getColumnName());
            holder.tv_style_item.setTextColor(Color.BLUE);
        }
        if (bean.getColumnId().equals("70")) {
            holder.tv_style_item.setText(bean.getColumnName());
            holder.tv_style_item.setTextColor(Color.RED);
        }
        if (bean.getColumnId().equals("102")) {
            holder.tv_style_item.setText(bean.getColumnName());
            holder.tv_style_item.setTextColor(Color.BLUE);
        }
        if (bean.getColumnId().equals("103")) {
            holder.tv_style_item.setText(bean.getColumnName());
            holder.tv_style_item.setTextColor(Color.BLUE);
        }
        if (bean.getColumnId().equals("110")) {
            holder.tv_style_item.setText(bean.getColumnName());
            holder.tv_style_item.setTextColor(Color.BLUE);
        }

        if (newsItemCliclListener!=null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int pos=holder.getLayoutPosition();
                    newsItemCliclListener.onItemClick(pos);
                }
            });
        }
    }

    @Override
    public int getItemCount()   {
        return allNews.size();
    }


    public class ViewHolder extends RecyclerView.ViewHolder {
        private ImageView iv_imgage_item;
        private TextView tv_content_item, tv_author_item, tv_time_item, tv_style_item;

        public ViewHolder(View itemView) {
            super(itemView);
            iv_imgage_item = (ImageView) itemView.findViewById(R.id.iv_image_item_news);
            tv_content_item = (TextView) itemView.findViewById(R.id.tv_content_item_news);
            tv_author_item = (TextView) itemView.findViewById(R.id.tv_author_item_news);
            tv_time_item = (TextView) itemView.findViewById(R.id.tv_time_item_news);
            tv_style_item = (TextView) itemView.findViewById(R.id.tv_style_item_news);
        }
    }
}
